<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<span>字符串 (1 2 3)</span><input type="text" value="2,3,4,1" id="text"><input type="button" value="求和" onclick="sum()">


</body>
<script>
    var elementById = document.getElementById("text");

    function sum() {
        var arr = elementById.value.split(",");
        var sum = 0;
        for (var i = 0; i < arr.length; i++) {
            sum+=parseInt(arr[i]);
        }
        alert(sum)

    }
</script>
</html>